package com.example.easybuy.controller;

import com.example.easybuy.annotaion.CheckPermission;
import com.example.easybuy.entity.News;
import com.example.easybuy.service.NewsService;
import com.example.easybuy.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/getList")
    @CheckPermission({"1","0"})
    public AjaxResult getList(@RequestParam Map<String, Object> map) {
        PageInfo<News> pageInfo = newsService.newsByPage(map);
        return AjaxResult.success().add("pageInfo", pageInfo);
    }

    @RequestMapping("/delNews")
    @CheckPermission({"1"})
    public AjaxResult delNews(int id) {
        return AjaxResult.success().add("status",newsService.delNews(id));
    }

    @RequestMapping("/addNews")
    @CheckPermission({"1"})
    public AjaxResult addNews(@RequestParam Map<String, Object> news) {
        return AjaxResult.success().add("status",newsService.addNews(news));
    }

    @RequestMapping("/updateNews")
    @CheckPermission({"1"})
    public AjaxResult updateNews(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("status",newsService.updateNews(map));
    }

    @RequestMapping("/getNews")
    @CheckPermission({"1","0"})
    public AjaxResult getNews(int id){
        return AjaxResult.success().add("status",newsService.getNews(id));
    }
}